package com.huiminpay.merchant.controller;
import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.merchant.common.intercept.util.SecurityUtil;
import com.huiminpay.transaction.api.dto.PayChannelDTO;
import com.huiminpay.transaction.api.dto.PayChannelParamDTO;
import com.huiminpay.transaction.api.dto.PlatformChannelDTO;
import com.huiminpay.transaction.api.service.PayChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "商户平台-渠道和支付参数相关", tags = "商户平台-渠道和支付参数", description = "商户平台-渠道和支付参数相关")
@RestController
public class PlatformChannelController {
    @Reference
    private PayChannelService payChannelService;
    @ApiOperation("获取平台服务类型")
    @GetMapping("/my/platform-channels")
    public List<PlatformChannelDTO> queryPlatformChannel() throws BusinessException {
        List<PlatformChannelDTO> platformChannelDTOS = payChannelService.queryPlatformChannel();
        return platformChannelDTOS;
    }
    @ApiOperation("应用绑定平台服务类型")
    @PostMapping(value="/my/apps/{appId}/platform-channels")
    public void isBindPlatform(@PathVariable("appId") String appId, @RequestParam("platformChannelCodes")
            String platformChannelCodes) throws BusinessException {
        payChannelService.isBindPlatform(appId,platformChannelCodes);
    }
    @ApiOperation("查询应用是否绑定了某个服务类型")
    @ApiImplicitParams({@ApiImplicitParam(name = "appId",value = "应用id",required = true,
            dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "platformChannel",value = "服务类型",required = true,
                    dataType = "String",paramType = "query")})
    @GetMapping("/my/merchants/apps/platformchannels")
    public int queryisBindPlatform(@RequestParam String appId, @RequestParam String platformChannel)
            throws BusinessException {
        int num = payChannelService.queryIsBind(appId, platformChannel);
        return num;
    }
    @ApiOperation("根据平台服务类型获取支付渠道列表")
    @ApiImplicitParam(name="platformChannelCode",value = "平台服务类型",dataType = "String",required = true,paramType = "path")
    @GetMapping("/my/pay-channels/platform-channel/{platformChannelCode}")
    public List<PayChannelDTO> queryPayChannelByPatformChannel(@PathVariable String platformChannelCode)
            throws BusinessException {
     return payChannelService.queryPayChannelByPatformChannel(platformChannelCode);
    }

    @ApiOperation("商户配置支付渠道参数")
    @RequestMapping(value = "/my/pay-channel-params",method = {RequestMethod.POST,RequestMethod.PUT})
    public void creatPayChannelParam(@RequestBody PayChannelParamDTO payChannelParamDTO) throws BusinessException {
        Long merchantId = SecurityUtil.getMerchantId();
        payChannelParamDTO.setMerchantId(merchantId);
        payChannelService.savePayChannelParam(payChannelParamDTO);
    }
    @ApiOperation("获取指定应用指定服务类型下所包含的原始支付渠道参数列表")
    @GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}")
    public List<PayChannelParamDTO> queryListPayChannelParamByappIdandPlatformChannel(
            @PathVariable String appId,@PathVariable String platformChannel) throws BusinessException {
        List<PayChannelParamDTO> payChannelParamDTOS = payChannelService.
                queryListPayChannelParamByappIdandPlatformChannel(appId, platformChannel);
        return payChannelParamDTOS;

    }

    @ApiOperation("获取指定应用指定服务类型下所包含的某个原始支付参数")
    @GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}/pay-channels/{payChannel}")
    public PayChannelParamDTO queryPayChannelParamByappIdandPlatformChannel(@PathVariable String appId,
                                                                            @PathVariable String platformChannel,
                                                                    @PathVariable String payChannel) throws BusinessException {
        PayChannelParamDTO payChannelParamDTO = payChannelService.
                queryPayChannelParamByappIdandPlatformChannel(appId, platformChannel, payChannel);
        return payChannelParamDTO;

    }






    }
